package indi.imooc.pecs;

import java.util.ArrayList;
import java.util.List;

public class PECS {

    class Fruit{};
    class Apple extends Fruit{};
    class RedApple extends Apple{};
    class Banana extends Fruit{};

    class Produce<E>{
//        public void product(List<E> list){
//            for(E e : list){
//                System.out.print("pruduce " + e.toString() + " ");
//                System.out.println();
//            }
//        }
        public void product(List<? extends E> list){
            for(E e : list){
                System.out.print("pruduce " + e.toString() + " ");
                System.out.println();
            }
        }


    }

    public void testProduce(){
        Produce<Fruit> produce = new Produce<>();
        List<Apple> list = new ArrayList<>();
        for (int i = 0; i < 10; i++){
            list.add(new Apple());
        }
        produce.product(list);
    }

    class Consumer<E>{
//        public E consume(List<E> lsit){
//            E e = lsit.get(0);
//            return e;
//
//        }
        public E consume(List<? super E> lsit){
            E e = (E)lsit.get(0);
            return e;
        }

    }

    public void testConsume(){
        Consumer<RedApple> consumer = new Consumer<>();
        List<Apple> list = new ArrayList<>();
        for (int i = 0; i < 10; i++){
            list.add(new RedApple());
        }
        Fruit fruit = consumer.consume(list);
        RedApple redApple = consumer.consume(list);
    }

    public static void main(String[] args) {
        PECS pecs = new PECS();
//        pecs.testProduce();

//        Produce<Banana> bananaProduce = pecs.new Produce<>();
//        List<Banana> bananaList = new ArrayList<>();
//        for (int i = 0; i < 10; i++){
//            bananaList.add(pecs.new Banana());
//        }
//        bananaProduce.produce(bananaList);





    }

/*    public void pecs(){
        List<? extends Fruit> fruits = new ArrayList<>();
        fruits = new ArrayList<Fruit>();
        fruits = new ArrayList<Apple>();
        fruits = new ArrayList<Banana>();
        fruits = new ArrayList<RedApple>();
        fruits = new ArrayList<Object>(); // 编译不通过

        fruits.add(new Apple());
        fruits.add(new Fruit());
        fruits.add(new Object());
        fruits.add(null);

        List<? super Apple> apples = new ArrayList<>();
        apples = new ArrayList<Fruit>();
        apples = new ArrayList<Banana>();
        apples = new ArrayList<RedApple>();

        apples.add(new RedApple());
        apples.add(new Banana());
        apples.add(new Fruit());

        Fruit fruit = (Fruit) apples.get(0);
        RedApple redApple = (RedApple) apples.get(0);

    }*/


}
